package modes

import (
	"fmt"
	"time"
)

/*
 * 描述：员工表
 *
 ********************************************************************************************/
type Employee struct {
	Id     int64  `json:"emp_id" xorm:"id"`           // 员 工 id
	RoleId int64  `json:"emp_role_id" xorm:"role_id"` // 角 色 id
	TeamId int64  `json:"emp_team_id" xorm:"team_id"` // 小 组 id
	ProjId int64  `json:"emp_proj_id" xorm:"proj_id"` // 项 目 id
	Name   string `json:"emp_name" xorm:"name"`       // 员工名称
	Phone  string `json:"emp_phone" xorm:"phone"`     // 账    号
	Pass   string `json:"-" xorm:"pass"`              // 密    码
	TQAcc  int    `json:"emp_tq_acc" xorm:"tq_acc"`   // TQ座席编号
	TQPw   string `json:"emp_tq_pw" xorm:"tq_pw"`     // TQ 秘 密
	Start  int    `json:"emp_start" xorm:"start"`     // 状    态
	At     int64  `json:"emp_at" xorm:"at"`           // 创建时间

}

type CallTotalUser struct {
	Employee `xorm:"extends"`
	Total    int64 `json:"time" xorm:"-"`
}

func (this *Employee) TableName() string {
	return "employee"
}

func (this *Employee) Save() (int64, error) {
	this.At = time.Now().Unix()
	return Db(2).Insert(this)
}
func (this *Employee) GetName(uid string) (bool, error) {
	shwere := fmt.Sprintf(" name like '%%%s'", uid)
	return Db(2).Where(shwere).Get(this)
}

func (this *Employee) Get() (bool, error) {
	return Db(2).Get(this)
}
func (this *Employee) GetRole() (bool, error) {
	where := "start = 0"
	if _, err := Db(2).Where(where).Get(this); err != nil {
		return false, err
	}
	if this.RoleId != 0 {
		if this.RoleId%100 == 1 {
			return true, nil
		}
		if this.RoleId%100 == 2 {
			return true, nil
		}
		return false, nil
	}
	return true, nil
}

func (this *Employee) update(where string, field string) (int64, error) {
	return Db(2).Where(where).Cols(field).Update(this)
}

func (this *Employee) IdSet(field string) (int64, error) {
	where := fmt.Sprintf("id = %d", this.Id)
	return this.update(where, field)
}

func (this *Employee) where(fage, count, page int, where, field string) ([]Employee, error) {
	list := make([]Employee, 0)
	var err error
	if field == "" {
		field = "id"
	}
	if 0 == fage { // 从小到大排序
		err = Db(2).Where(where).
			Asc(field).
			Limit(count, page*count).
			Find(&list)
	} else { // 从大到小排序
		err = Db(2).Where(where).
			Desc(field).
			Limit(count, page*count).
			Find(&list)
	}
	return list, err
}

// 删除工作池中的数据
func (this *Employee) DelWorkPhone(phone int64) (int, error) {
	var field WorkFirs
	if _, err := field.PhoneDel(phone); nil == err {
		//if count > 0 {
		//	return 1, nil
		//}
		var pool WorkPool
		if _, err := pool.PhoneDel(phone); err != nil {
			return 2, nil
		}
		var susing Using
		if _, err := susing.PhoneDel(phone); err != nil {
			return 3, nil
		} else {
			return 0, err
		}
	} else {
		return 0, err
	}
}

//获取列表
func (this *Employee) GetListByName(val *Pager) error {
	where := fmt.Sprintf(" name like  '%%%s%%' and start = 0", this.Name)
	total := this.getTotal()
	val.Total = total
	list, err := this.where(1, val.Count, val.Page, where, "id")
	if err != nil {
		return err
	}
	val.List = list
	return err
}

func (this *Employee) getTotal() int64 {

	where := fmt.Sprintf(" name like  '%%%s%%' and start = 0 ", this.Name)
	total, _ := Db(1).Table(this.TableName()).Where(where).Count()
	return total
}
